package com.example.wms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.classes.Database;
import com.example.classes.GlobalApplication;
import com.example.classes.Member;

import java.util.Collection;
import java.util.Hashtable;

public class StartupActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    public final static String USER_ID="com.example.wms.UNAME";
    public final static String PASS = "com.example.wms.UPASS";
    Hashtable<String, Integer> loginRecord = new Hashtable<String, Integer>();
    Database db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        GlobalApplication ga = (GlobalApplication)getApplicationContext();
        db = ga.getDb();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DataBase db=new DataBase(this);
        db.open();
        Collection<Member> listofMembers=db.getAllUsers();
        for(Member m: listofMembers)
        {
            Log.v("STARTUP ACTIVITY",m.toString());
        }

    }
    public void tryLogin(View view)
    {
        Intent intent= new Intent(this, LoginHandler.class);
        EditText loginID= (EditText)findViewById(R.id.uid_enter);
        EditText pass= (EditText) findViewById(R.id.upass_enter);
        intent.putExtra(USER_ID,loginID.getText().toString() );
        intent.putExtra(PASS,pass.getText().toString());
        String un = loginID.getText().toString();
        if (loginRecord.containsKey(un))
        {
            Log.v("Startup", "***** This user name exists; adding 1 to val.");
            int val = loginRecord.get(un);
            val++;
            loginRecord.put(un, val);
        }
        else
        {
            Log.v("Startup", "***** Didn't contain this user name; adding it to the record.");
            loginRecord.put(un, 0);
        }
        if (loginRecord.get(un) >= 3)
        {
            Log.v("Startup", "Blocked the shit out of you.");
            Toast.makeText(StartupActivity.this, "That account has been locked!", Toast.LENGTH_LONG).show();
        }
        else
        {
            Log.v("Startup", "Starting intent");
            startActivity(intent);
        }
    }
    public void registerClicked(View view)
    {
        Intent intent=new Intent(this, registerActivity.class);
        startActivity(intent);
    }
}
